package com.breaktian.demoapp.ui.refreshdemo;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.breaktian.assemble.refresh.superrefresh.PullDownRefreshLayout;
import com.breaktian.assemble.utils.ThreadUtil;
import com.breaktian.demoapp.R;

import java.util.ArrayList;
import java.util.List;

public class ListViewRefreshActivity extends AppCompatActivity {

    private PullDownRefreshLayout mPullRefreshLayout;
    private ListView listView;
    private ArrayList<String> list;
    private ListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view_refresh);

        mPullRefreshLayout = (PullDownRefreshLayout)findViewById(R.id.pullRefreshLayout);

        mPullRefreshLayout.setOnPulldownRefreshListener(new PullDownRefreshLayout.OnPullDownRefreshListener() {
            @Override
            public void onPullDown() {
                ThreadUtil.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mPullRefreshLayout.stopPullDownRefresh();
                    }
                },1500);
            }

        });
        listView = (ListView)findViewById(R.id.listView);
        list = new ArrayList<String>();
        for(int i=0;i<40;i++){
            list.add("hello word "+i);
        }

        adapter = new ListAdapter(this,list);
        listView.setAdapter(adapter);

    }



    public class ListAdapter extends BaseAdapter{

        private final List<String> list;
        private final Context context;

        public ListAdapter(Context context, List<String> list){
            this.context = context;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv = new TextView(context);
            tv.setTextColor(Color.parseColor("#000000"));
            tv.setTextSize(16);
            tv.setText(list.get(position));
            return tv;
        }



    }

}
